package com.prince.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.prince.aspect.AppLog;
import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.model.Customer;
import com.prince.model.Supplier;
import com.prince.page.Page;
import com.prince.util.JsonUtil;
import com.prince.util.PoiExcelExportUtil;
import com.prince.web.base.BaseController;
import com.prince.web.base.SessionUtil;
import com.prince.web.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 供应商
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/6 14:26
 */
@Controller
@Validated
public class SupplierController extends BaseController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 功能描述: 分页查询供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findSupplierByPage")
    public ResponseEntity<Map<String,Object>> findSupplierByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = supplierService.findSupplierByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }

    /**
     * 功能描述: 添加供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/addSupplier")
    public ResponseEntity<Map<String,Object>> addSupplier(Supplier supplier) {
        supplier.setCreater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.addSupplier(supplier);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 修改供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/updateSupplier")
    public ResponseEntity<Map<String,Object>> updateSupplier(Supplier supplier) {
        supplier.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.updateSupplier(supplier);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 删除供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/delSupplier")
    public ResponseEntity<Map<String,Object>> delSupplier(Supplier supplier) {
        supplier.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.delSupplier(supplier);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 通过id查询供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/findSupplierById")
    public ResponseEntity<Map<String,Object>> findSupplierById(@NotNull Integer  id) {
        Map<String,Object> map = supplierService.findSupplierById(id);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 查询所有供应商
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/findSupplierAll")
    public ResponseEntity<Map<String,Object>> findSupplierAll() {
        Map<String,Object> map = supplierService.findSupplierAll();
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }
    /**
     * 功能描述: 通过拼音码查询供应商资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @ResponseBody
    @PostMapping("/findSupplierByPinyin")
    public List<Supplier> findSupplierByPinyin(@NotNull String pinyin) {
        return supplierService.findSupplierByPinyin(pinyin);
    }
    /**
     * 功能描述: 导出供应商资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportSupplier")
    public void exportSupplier(@RequestParam Map<String,Object> params,HttpServletResponse response){
        Map<String,Object> map = supplierService.findSupplierByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<Supplier> list = (List<Supplier>)object;
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {  "supplierName","name","addr","phone","fax","postalCode", "pinyin","created"};
        String[] titleNames = {"供应商名称","联系人", "地址","手机号","传真","邮编"+"拼音码", "时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "供应商资料-" + strDate);

    }
    /**
     * 功能描述: 分页查询客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findCustomerByPage")
    public ResponseEntity<Map<String,Object>> findCustomerByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = supplierService.findCustomerByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }

    /**
     * 功能描述: 添加客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/addCustomer")
    public ResponseEntity<Map<String,Object>> addCustomer(Customer customer) {
        customer.setCreater(SessionUtil.getSession().getLoginName());
        customer.setUpdater(customer.getCreater());
        Map<String,Object> map = supplierService.addCustomer(customer);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 修改客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/updateCustomer")
    public ResponseEntity<Map<String,Object>> updateCustomer(Customer customer) {
        customer.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.updateCustomer(customer);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 删除客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/delCustomer")
    public ResponseEntity<Map<String,Object>> delCustomer(Customer customer) {
        customer.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.delCustomer(customer);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 通过id查询客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/findCustomerById")
    public ResponseEntity<Map<String,Object>> findCustomerById(@NotNull Integer id) {
        Map<String,Object> map = supplierService.findCustomerById(id);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 通过拼音码查询客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/findCustomerByPinyin")
    public void findCustomerByPinyin(@NotNull String pinyin, HttpServletRequest request, HttpServletResponse response) {
        List<Customer> list = supplierService.findCustomerByPinyin(pinyin);
        try {
            response.setContentType("text/plain");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("text/html; charset=utf-8");
            Map<String,String> map = new HashMap<String,String>();
            map.put("result", "content");
            PrintWriter out = response.getWriter();
            String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数
            out.println(jsonpCallback+"("+JsonUtil.toJSONString(list)+")");//返回jsonp格式数据
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述: 导出客户资料
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportCustomer")
    public void exportCustomer(@RequestParam Map<String,Object> params,HttpServletResponse response){
        Map<String,Object> map = supplierService.findCustomerByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<Customer> list = (List<Customer>)object;
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {  "name","phone","addr","amount","totalSpending","totalRevenue", "pinyin","created"};
        String[] titleNames = {"姓名","手机号", "地址","金额","总支出","总充值","拼音码", "时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "客户资料-" + strDate);

    }

    /**
     * 功能描述: 充值金额
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/addCustomerAmount")
    public ResponseEntity<Map<String,Object>> addCustomerAmount(Customer customer) {
        customer.setCreater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = supplierService.addCustomerAmount(customer);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 分页查询流水
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/5/28 18:02
     */
    @AppLog
    @PostMapping("/findcustomerOrderByPage")
    public ResponseEntity<Map<String,Object>> findcustomerOrderByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = supplierService.findcustomerOrderByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }

}
